<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>药方解析测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-input { width: 100%; height: 100px; margin: 10px 0; }
        .result { margin: 20px 0; padding: 15px; background: #f8f9fa; border-radius: 5px; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
    </style>
</head>
<body>
    <h1>药方解析测试</h1>
    
    <div>
        <label for="testInput">测试输入：</label>
        <textarea id="testInput" class="test-input">西洋参90g别直参60g柴胡90g当归90g，怀牛膝90g，白茄根90g，紫丹参150g，白芍90g，巴戟天90g，山茱萸90g，泽兰90g，菟丝子90g，枸杞子90g，潼沙苑90g，仙茅90g，熟地黄300g（砂仁同拌）砂仁，仙灵脾150g，制首乌150g，玉竹180g，黄芪300g</textarea>
    </div>
    
    <button onclick="testParsing()">测试解析</button>
    
    <div id="result" class="result"></div>

    <script>
        function testParsing() {
            const inputText = document.getElementById('testInput').value;
            const resultDiv = document.getElementById('result');
            
            // 预处理输入文本：移除括号内容、注释等
            let cleanedText = inputText
                .replace(/（[^）]*）/g, '') // 移除中文括号内容
                .replace(/\([^)]*\)/g, '') // 移除英文括号内容
                .replace(/[，,]/g, ',') // 统一逗号
                .replace(/\s+/g, ' ') // 统一空格
                .trim();
            
            // 清理输入文本，支持多种分隔符
            const lines = cleanedText.split(/[,，\s\n]+/).filter(line => line.trim());
            let successCount = 0;
            let errorCount = 0;
            let processedLines = [];
            
            let resultHtml = `
                <h3>解析结果</h3>
                <p><strong>原始输入:</strong> ${inputText}</p>
                <p><strong>清理后文本:</strong> ${cleanedText}</p>
                <p><strong>分割后行数:</strong> ${lines.length}</p>
                <hr>
                <h4>详细解析:</h4>
            `;
            
            lines.forEach((line, index) => {
                const trimmedLine = line.trim();
                if (!trimmedLine) return;
                
                // 解析药材名和用量
                let herbName, weight;
                
                // 尝试匹配 "药材名+数字+g" 格式
                const match1 = trimmedLine.match(/^(.+?)(\d+(?:\.\d+)?)g$/);
                if (match1) {
                    herbName = match1[1].trim();
                    weight = parseFloat(match1[2]);
                } else {
                    // 尝试匹配 "药材名+数字" 格式
                    const match2 = trimmedLine.match(/^(.+?)(\d+(?:\.\d+)?)$/);
                    if (match2) {
                        herbName = match2[1].trim();
                        weight = parseFloat(match2[2]);
                    } else {
                        // 默认格式，假设整个字符串都是药材名
                        herbName = trimmedLine;
                        weight = 100; // 默认用量100g
                    }
                }
                
                // 清理药材名称（移除多余空格、特殊字符等）
                herbName = herbName.replace(/^\s+|\s+$/g, '').replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '');
                
                if (herbName && weight > 0) {
                    processedLines.push({ herbName, weight, originalLine: trimmedLine });
                    successCount++;
                    resultHtml += `<p class="success">✓ ${index + 1}. ${herbName} - ${weight}g (原文本: ${trimmedLine})</p>`;
                } else {
                    errorCount++;
                    resultHtml += `<p class="error">✗ ${index + 1}. 格式错误: ${trimmedLine}</p>`;
                }
            });
            
            resultHtml += `
                <hr>
                <h4>统计信息:</h4>
                <p><strong>成功解析:</strong> ${successCount} 个药材</p>
                <p><strong>解析失败:</strong> ${errorCount} 个</p>
                <p><strong>总计:</strong> ${lines.length} 个输入项</p>
            `;
            
            resultDiv.innerHTML = resultHtml;
        }
        
        // 页面加载时自动测试
        window.onload = testParsing;
    </script>
</body>
</html>
